package com.example.testapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;

import com.example.testapplication.service.MusicService;

public class serviceMusic extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_music);
        // 设置全屏显示
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        ImageButton btn_play = (ImageButton) findViewById(R.id.btn_play); // 获取播放/停止 按钮

        // 启动服务与停止服务 实现播放背景音乐与停止播放背景音乐
        btn_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(MusicService.isplay == false){ //判断音乐播放的状态
                    // 启动服务，从而实现播放背景音乐
                    startService(new Intent(serviceMusic.this,MusicService.class));
                    // 替换播放背景音乐图标
                    ((ImageButton) view).setImageDrawable(getResources().getDrawable(R.drawable.bgplay,null));

                }else{
                    // 停止服务，从而实现停止播放背景音乐
                    stopService(new Intent(serviceMusic.this,MusicService.class));
                    ((ImageButton) view).setImageDrawable(getResources().getDrawable(R.drawable.bgs,null));
                }
            }
        });
    }
    @Override
    protected void onStart() {  //实现进入界面时，启动背景音乐服务
        //启动服务，从而实现播放背景音乐
        startService(new Intent(serviceMusic.this, MusicService.class));
        super.onStart();
    }
}